function [xmlS, xml, url, xmlS_2] = queryFeatures(hObject,handles)
% QUERYFEATURES used by DASMiner Browser for managing a DAS feature query
% 
% SYNTAX: queryFeatures(handles)
%          handles - handles of the DAS_GUI graphical objects
%
% See also DAS_GUI
%  
%    DASMiner: DAS library and browser for Matlab.
%    Diogo Veiga, March 2008.


if (get(handles.popupmenu_SegDef,'Value') == 1)
    msgbox('Define the segment type.','Segment Type Requested','warn');
    return
end

str = get(handles.popupmenu_SegDef,'String');
val = get(handles.popupmenu_SegDef,'Value');
dataType = str{val};

if (any(strcmpi(dataType,{'Chromosome','Clone', 'Contig', 'NT_Contig', 'Scaffold'})) ) %DNA segment

    if (isempty(get(handles.field_Chrom,'String')) || isempty(get(handles.field_Start,'String')) ...
            || isempty(get(handles.field_Stop,'String')) )
        msgbox('Fill out the fields Chromosome, Start, Stop.','Data Requested','warn');
        return
    end
    
    if (get(handles.popupmenu_FeatOptions2,'Value') == 1)
        msgbox('Make a choice in the Options menu','Data Requested','warn');
        return
    end

    if (get(handles.popupmenu_FeatOptions2,'Value') == 2) %All features query
        hDial = openRunningDialog;
        [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'features', 'DSN', handles.dsn ,'chrom',get(handles.field_Chrom,'String'), ...
            'start',get(handles.field_Start,'String'), 'stop', get(handles.field_Stop,'String'));
        setResultsFields(hObject,handles,xml,url);
        delete(hDial);
        clear hDial;

    elseif (get(handles.popupmenu_FeatOptions2,'Value') == 3) %Specifying features query

        [featList,okay] = getFeaturesSelection(handles);

        if (~okay), return; end
        hDial = openRunningDialog;
        [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'features', 'DSN', handles.dsn ,'chrom',get(handles.field_Chrom,'String'), ...
            'start',get(handles.field_Start,'String'), 'stop', get(handles.field_Stop,'String'),'featuresList', featList);
        setResultsFields(hObject,handles,xml,url);
        delete(hDial);
        clear hDial;
    end
    
else %ID definition

    if (isempty(get(handles.field_ID,'String')))
        msgbox('Fill out ID','Data Requested','warn');
        return
    end
    
    if (get(handles.popupmenu_FeatOptions,'Value') == 1)
        msgbox('Make a choice in the Options menu','Data Requested','warn');
        return
    end
    
    if (get(handles.popupmenu_FeatOptions,'Value') == 2) %All features query
        hDial = openRunningDialog;
        [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'features', 'DSN', handles.dsn ,'segment',get(handles.field_ID,'String'));
        setResultsFields(hObject,handles,xml,url);
        delete(hDial);
        clear hDial;

    elseif (get(handles.popupmenu_FeatOptions,'Value') == 3) %Specifying features query

        [featList,okay] = getFeaturesSelection(handles);

        if (~okay), return; end
        hDial = openRunningDialog;
        [xmlS, xml, status,url,xmlS_2] = executeDasCommand(handles.baseUrl,'command', 'features', 'DSN', handles.dsn ,'segment',get(handles.field_ID,'String'), ...
            'featuresList', featList);
        setResultsFields(hObject,handles,xml,url);
        delete(hDial);
        clear hDial;

    end
    
end
guidata(hObject,handles);

function [featList,status] = getFeaturesSelection(handles)

contents = get(handles.listbox_Feat,'String');
ind = get(handles.listbox_Feat,'Value');
nFeat = size(ind,2);

featList = cell(nFeat,1);
[featList{:}] = contents{ind};
status = 1;

featList = featList';


if ( (nFeat == 1) && (ind(1)==1) )
    msgbox('Choose the features for the query.','Data Requested','warn');
    status = 0;
    return
end

function [hDial] = openRunningDialog
hDial = showinfowindow({'Connecting to the server ...','Running request...'},'Please wait ...');
set(hDial,'CloseRequestFcn','');
t = get(hDial,'Position');
set(hDial,'Position',t)

function setResultsFields(hObject,handles,xml,url)

set(handles.field_XMLDisp,'String',xml);
handles.currentXML = xml;
set(handles.field_queryURL,'String',url);
handles.currentURL = url;
guidata(hObject,handles);
